package midalgorithms.sortandsearch;

public class _01SortColors {
    public static int[] sortColors(int[] nums) {
        //  数组中只存在三种元素，即0、1、2
        //  左右指针法
        int left = 0,right = nums.length-1;
        //  遍历数组
        for (int i = 0;i <= right;i++){
            if (nums[i] == 0){
                //  挪到数组左端
                swap(nums,i,left);
                left++;
            }
            if (nums[i] == 2){
                //  挪到数组右端
                swap(nums,i,right);
                right--;
                i--;
            }
        }
        return nums;
    }
    private static void swap(int[] nums,int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
